<?php

namespace app\controllers;

use app\common\utils\Qyh;
use app\common\utils\QyhServer;
use app\models\GkyQyh;
use app\models\GkyQyhApp;
use Yii;
use app\models\GkyQyhMenu;
use app\models\GkyQyhMenuSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * GkyQyhMenuController implements the CRUD actions for GkyQyhMenu model.
 */
class GkyQyhMenuController extends Controller
{
     public $layout='main_1';
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all GkyQyhMenu models.
     * @return mixed
     */
    public function actionIndex($id)
    {
        $app = GkyQyhApp::findOne(['id'=>$id]);

        $searchModel = new GkyQyhMenuSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $app->agentid);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'app' => $app
        ]);
    }

    /**
     * Displays a single GkyQyhMenu model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new GkyQyhMenu model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new GkyQyhMenu();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing GkyQyhMenu model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing GkyQyhMenu model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the GkyQyhMenu model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return GkyQyhMenu the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = GkyQyhMenu::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

    public function actionSetMenu(){
        $qyhid=  \Yii::$app->request->post('qyhid');
        $accountid=  \Yii::$app->request->post('accountid');
        $agentid = \Yii::$app->request->post('agentid');
        $gkyQyh = GkyQyh::findOne(['id'=>$qyhid]);
        $qyh = new QyhServer($gkyQyh);
        $result = $qyh->setMenu($accountid,$agentid);
        if($result->errcode == 0){
            echo json_encode(['status'=>0]);
        }else{
            echo json_encode(['status'=>-1]);
        }

    }
}
